addEvent("onload", init);
addEvent("onresize", checkOrientation);

var screenmode = 0;

function init(){
    try {
        var myStyleTweaks = new StyleTweaker();
        myStyleTweaks.add("Series60/5.0", "styles/tweaks/S605th.css");
        myStyleTweaks.add("Series60/3.2", "styles/tweaks/S603rdFP2.css");
        myStyleTweaks.tweak();
        
        initService();
        listMessages();
    } 
    catch (e) {
        alert(e.toString());
    }
}

function checkOrientation(){
    try {
        var width = parseInt(screen.width);
        var height = parseInt(screen.height);
        
        //var landscapeTweaker = new StyleTweaker();
        // Landscape tweaks for template styles not available in templates?
        //landscapeTweaker.add("Series60/5.0", "../resources/styles/tweaks/S605thLandscape.css");
        
        if (width > height) {
        
            // if already in landscape mode, return
            if (screenmode == 1) 
                return;
            screenmode = 1;
            // landscapeTweaker.tweak();
        }
        else {
            // if already in portrait mode, return
            if (screenmode == 0) 
                return;
            screenmode = 0;
            //        landscapeTweaker.untweak();
        }
    } 
    catch (e) {
        alert(e.toString());
    }
}

/*
 * Shows or hides messages inbox container
 * f - flag: if true - inbox is shown, otherwise smsSendContainer is shown
 */
function showInbox(f){
    if (f) {
		document.getElementById("a1").className="";
		document.getElementById("a2").className="selectedTab";
        document.getElementById("smsSendContainer").style.display = "none";
        document.getElementById("inboxContainer").style.display = "block";
    }
    else {
		document.getElementById("a1").className="selectedTab";
		document.getElementById("a2").className="";
        document.getElementById("smsSendContainer").style.display = "block";
        document.getElementById("inboxContainer").style.display = "none";
    }
}

/*
 * Sets specified status to all selected messages
 * status - status to be set: read, unread, delete
 * delete is considered as message status, but on practice message is removed from database
 */
function setStatus(status){
    var msgs = document.msg1.ch;
    if (msgs == null) 
        return;
    if (status == 'delete') {
        if (!confirm('Delete selected message(s)?')) 
            return;
    }
    if (msgs.length) {
        for (var i = 0; i < msgs.length; i++) {
            if (msgs[i].checked) {
                setMessageStatus(msgs[i].value, status);
            }
        }
    }
    else 
        setMessageStatus(msgs.value, status);
    
    listMessages();
}

/*
 * Sets all message checkboxes to specified status
 * checked - status to be set: true or false
 */
function chALL(checked){
    var items = document.msg1.ch;
    if (items == null) 
        return;
    if (items.length) {
        for (var i = 0; i < items.length; i++) {
            items[i].checked = checked;
        }
    }
    else 
        items.checked = checked;
}

/*
 * Sets 'all' checkbox to 'checked' status if all massages checkboxes are checked
 */
function checkMain(){
    var items = document.msg1.ch;
    if (items == null) 
        return;
    var checked = true;
    if (items.length) {
        for (var i = 0; i < items.length; i++) {
            checked = checked && items[i].checked;
        }
    }
    else 
        checked = items.checked;
    document.msg1.chAll.checked = checked;
}

/*
 * Shows selected message details
 * id - unique identifier of message
 * Message information is retrieved by getMessage(id) function defined in platformservices_20_specific.js
 * Message Info schema:
 *    {
 *	    "message": {message data},
 *      "sender": String,
 *	    "time":  Date,
 *    	"unread": Boolean,
 *      "id": String // Identifies message in the message store
 *    }
 */
function openMessage(id){
    var message = getMessage(id);
    if (message != null) {
        var txt = "Sender: " + message.sender + "\n";
        txt += "Time: " + message.time + "\n";
        txt += "Status: " + (message.unread ? "unread" : "read") + "\n";
        txt += message.message.body;
        alert(txt);
    }
    else 
        alert("Error retrieving message info.");
}
